﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SQLite;

namespace CS.DAL
{
    // 提供执行方法
    public class SqLiteHelper
    {
        // 连接字符串
        static string connStr = ConfigurationManager.ConnectionStrings["SQLite"].ConnectionString;

        /// <summary>
        /// 实现非查询操作，增删改返回受影响行数，否则返回-1
        /// </summary>
        /// <param name="SQLite"></param>
        /// <param name="ps"></param>
        /// <returns></returns>
        public static int ExecuteNonQuery(string SQLite, CommandType type, params SQLiteParameter[] ps)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                using (SQLiteCommand cmd = new SQLiteCommand(SQLite, conn))
                {
                    cmd.Parameters.AddRange(ps);
					cmd.CommandType = type;
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public static object ExecuteScalar(string SQLite, CommandType type, params SQLiteParameter[] ps)
        {
            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                using (SQLiteCommand cmd = new SQLiteCommand(SQLite, conn))
                {
                    cmd.Parameters.AddRange(ps);
					cmd.CommandType = type;
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }

        public static SQLiteDataReader ExecuteReader(string SQLite, CommandType type, params SQLiteParameter[] ps)
        {
            SQLiteConnection conn = new SQLiteConnection(connStr);
            try
            {
                using (SQLiteCommand cmd = new SQLiteCommand(SQLite, conn))
                {
                    cmd.Parameters.AddRange(ps);
					cmd.CommandType = type;
                    conn.Open();
                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
            }
            catch (Exception ex)
            {
                conn.Dispose();
                throw ex;
            }
        }

        public static DataSet GetDataSet(string SQLite, CommandType type, params SQLiteParameter[] ps)
        {
            DataSet ds = new DataSet();
            using (SQLiteDataAdapter sda = new SQLiteDataAdapter(SQLite, connStr))
            {
                sda.SelectCommand.Parameters.AddRange(ps);
				sda.SelectCommand.CommandType = type;
                sda.Fill(ds);
            }
            return ds;
        }




        // 常用的简化方法
        public static int ExecuteNonQuery(string SQLite, params SQLiteParameter[] ps)
        {
            return ExecuteNonQuery(SQLite, CommandType.Text, ps);
        }
        public static int ExecuteNonQuerySP(string SQLite, params SQLiteParameter[] ps)
        {
            return ExecuteNonQuery(SQLite, CommandType.StoredProcedure, ps);
        }

        public static object ExecuteScalar(string SQLite, params SQLiteParameter[] ps)
        {
            return ExecuteScalar(SQLite, CommandType.Text, ps);
        }
        public static object ExecuteScalarSP(string SQLite, params SQLiteParameter[] ps)
        {
            return ExecuteScalar(SQLite, CommandType.StoredProcedure, ps);
        }

        public static SQLiteDataReader ExecuteReader(string SQLite, params SQLiteParameter[] ps)
        {
            return ExecuteReader(SQLite, CommandType.Text, ps);
        }
        public static SQLiteDataReader ExecuteReaderSP(string SQLite, params SQLiteParameter[] ps)
        {
            return ExecuteReader(SQLite, CommandType.StoredProcedure, ps);
        }

        public static DataSet GetDataSet(string SQLite, params SQLiteParameter[] ps)
        {
            return GetDataSet(SQLite, CommandType.Text, ps);
        }
        public static DataSet GetDataSetSP(string SQLite, params SQLiteParameter[] ps)
        {
            return GetDataSet(SQLite, CommandType.StoredProcedure, ps);
        }
    }
}
